<template>
  <div>
    <!--卡片-->
    <el-card class="box-card">
      <!-- 搜索 -->
      <el-form v-show="showSearch" :model="queryForm" :inline="true" label-width="68px">
        <el-form-item label="" prop="postName">
          <el-input v-model="queryForm.postName" placeholder="请输入名称" clearable size="small" prefix-icon="el-icon-search" />
        </el-form-item>
        <el-form-item>
          <el-button plain type="primary" icon="el-icon-search" size="mini" @click="searchQuery">搜索</el-button>
          <el-button plain type="warning" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 新增，编辑，删除，导出 -->
      <el-button
        v-hasPermi="['admin','system:post:add']"
        type="primary"
        small
        icon="el-icon-plus"
        size="mini"
        @click="addDialogVisible = true"
      >新增
      </el-button>
      <el-button
        v-hasPermi="['admin','system:post:edit']"
        :disabled="ids.length===1?false:true"
        type="success"
        small
        icon="el-icon-edit"
        style="margin:10px"
        size="mini"
        @click="editBtn(ids[0])"
      >修改
      </el-button>
      <el-button
        v-hasPermi="['admin','system:post:del']"
        :disabled="ids.length>0?false:true"
        type="danger"
        small
        icon="el-icon-delete"
        style="margin:10px"
        size="mini"
        @click="delAllBtn()"
      >删除
      </el-button>
      <el-button
        v-hasPermi="['admin','system:post:export']"
        type="warning"
        small
        icon="el-icon-download"
        style="margin:10px"
        size="mini"
        @click="exportClick"
      >导出
      </el-button>
      <!-- 搜索，刷新，表头工具栏 -->
      <right-bar :show-search.sync="showSearch" :table-data="tableColumns" @queryTable="refresh()" />

      <!--表格-->
      <el-table :data="listForm" stripe style="font-size: 12px;width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="hasTable('postId',tableColumns)" prop="postId" label="id" />
        <el-table-column v-if="hasTable('postName',tableColumns)" prop="postName" label="岗位名称" />
        <el-table-column v-if="hasTable('createTime',tableColumns)" prop="createTime" label="创建时间" />
        <!--操作-->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-hasPermi="['admin','system:post:edit']" type="text" size="mini" @click="editBtn(scope.row.postId)">编辑</el-button>
            <el-button v-hasPermi="['admin','system:post:del']" type="text" size="mini" @click="delBtn(scope.row.postId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页区域-->
      <el-pagination
        background
        :current-page="pageNumber"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <!--添加对话框-->
    <el-dialog title="添加" :close-on-click-modal="false" :visible.sync="addDialogVisible" width="580px" @close="addHandleClose">
      <span>
        <el-form ref="addFormRef" :model="addForm" :inline="true" :rules="addFormRules" label-width="100px">
          <el-form-item label="岗位名称" prop="postName">
            <el-input v-model="addForm.postName" />
          </el-form-item>
        </el-form>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="addDialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="addClick">确 定</el-button>
      </span>
    </el-dialog>

    <!--编辑对话框-->
    <el-dialog title="编辑" :close-on-click-modal="false" :visible.sync="editDialogVisible" width="580px" @close="editHandleClose">
      <span>
        <el-form ref="editFormRef" :model="editForm" :inline="true" label-width="100px">
          <el-form-item label="岗位名称" prop="postName">
            <el-input v-model="editForm.postName" />
          </el-form-item>
        </el-form>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="editDialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="editClick">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { add, edit, del, getSysPost, listSysPost, exportSysPost } from '@/api/system/post'
import { downloadFile } from '@/utils/index'

export default {
  name: 'SysPost',
  data() {
    return {
      addDialogVisible: false,
      editDialogVisible: false,
      showSearch: true,
      pageNumber: 1,
      pageSize: 10,
      total: 0,
      ids: [],
      listForm: [],
      addForm: {
        postId: '',
        postName: '',
        createTime: ''
      },
      queryForm: {
        postName: ''
      },
      editForm: {},
      tableColumns: [
        {
          label: 'id',
          value: 'postId',
          visible: true
        },
        {
          label: '岗位名称',
          value: 'postName',
          visible: true
        },
        {
          label: '创建时间',
          value: 'createTime',
          visible: true
        }
      ],
      addFormRules: {
        postId: [
          { required: true, message: '请输入id', trigger: 'blur' }
        ],
        postName: [
          { required: true, message: '请输入岗位名称', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '请输入创建时间', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 初始化列表
    this.initListSysPost()
  },

  methods: {
    // 查询按钮
    searchQuery() {
      this.searchListSysPost()
    },

    // 重置按钮
    resetQuery() {
      this.queryForm = {}
      this.initListSysPost()
    },

    // 刷新按钮
    refresh() {
      this.searchListSysPost()
    },

    // 添加按钮
    addBtn() {
      this.addDialogVisible = true
    },
    // 确认添加
    addClick() {
      this.$refs.addFormRef.validate(valid => {
        if (!valid) return
        add(this.addForm).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.searchListSysPost()
          }
          // 关闭对话框
          this.addDialogVisible = false
        })
      })
    },

    // 编辑按钮
    editBtn(id) {
      this.editDialogVisible = true
      getSysPost(id).then(res => {
        if (res.code === 20000) {
          this.editForm = res.data
        }
      })
    },
    // 确认编辑
    editClick() {
      this.$refs.editFormRef.validate(valid => {
        if (!valid) return
        edit(this.editForm).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.searchListSysPost()
          }
          // 关闭对话框
          this.editDialogVisible = false
        })
      })
    },

    // 批量删除按钮
    delAllBtn() {
      const data = {
        ids: this.ids
      }
      this.delSysPost(data)
    },
    // 删除按钮
    delBtn(id) {
      this.ids = []
      this.ids.push(id)
      const data = {
        ids: this.ids
      }
      this.delSysPost(data)
      this.ids = []
    },
    // 删除方法
    delSysPost(id) {
      this.$confirm('此操作将永久删除【' + this.ids.length + '条数据】, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认操作
        del(id).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.searchListSysPost()
          }
        })
      })
    },

    // 导出
    exportClick() {
      const tableName = 'sys_post'
      this.$confirm('是否确认导出所有用户数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认操作
        exportSysPost().then(data => {
          downloadFile(data, tableName, 'xlsx')
        })
      })
    },

    // 表格全选
    handleSelectionChange(val) {
      this.ids = []
      for (let index = 0; index < val.length; index++) {
        this.ids.push(val[index].postId)
      }
    },

    // 监听当前页的事件
    handleCurrentChange(pageIndex) {
      this.pageNumber = pageIndex
      this.searchListSysPost()
    },

    // 监听当前页的条数事件
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.searchListSysPost()
    },

    // 监听添加对话框的关闭事件，重置表单内容
    addHandleClose() {
      this.$refs.addFormRef.resetFields()
    },

    // 监听编辑对话框的关闭事件,重置表单内容
    editHandleClose() {
      this.$refs.editFormRef.resetFields()
    },

    // 条件查询
    searchListSysPost() {
      const page = {
        pageNum: this.pageNumber,
        pageSize: this.pageSize
      }
      listSysPost(this.queryForm, page).then(res => {
        this.listForm = res.data.records
        this.total = res.data.total
        this.pageNum = res.data.current
      })
    },

    // 初始化数据
    initListSysPost() {
      const page = {
        pageNum: this.pageNumber,
        pageSize: this.pageSize
      }
      listSysPost(null, page).then(res => {
        this.listForm = res.data.records
        this.total = res.data.total
        this.pageNum = res.data.current
      })
    }
  }
}
</script>

<style scoped>
</style>
